﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Web.Script.Serialization;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Reflection;
using System.Runtime.Serialization;

namespace MvcServiceLibrary
{
    public static class Helpers
    {
        public static ResponseType GetResponseType(this IHttpRequest request)
        {
            ResponseType result = ResponseType.None;

            // The requested format 
            // is specified via the querystring
            string format = request.QueryString["format"];

            if (String.IsNullOrEmpty(format))
            {
                if (request.AcceptTypes != null &&
                    request.AcceptTypes.Length > 0 &&
                    !String.IsNullOrEmpty(request.AcceptTypes[0]) &&
                    request.AcceptTypes[0].IndexOf("json", StringComparison.InvariantCultureIgnoreCase) > 0)
                    result = ResponseType.Json;
                else
                    // If no querystring was specified, 
                    // assume the default HTML format
                    result = ResponseType.Html;
            }
            else
            {
                if (String.Equals(format, "html", StringComparison.InvariantCultureIgnoreCase))
                {
                    result = ResponseType.Html;
                }
                else if (String.Equals(format, "xml", StringComparison.InvariantCultureIgnoreCase))
                {
                    result = ResponseType.Xml;
                }
                else if (String.Equals(format, "json", StringComparison.InvariantCultureIgnoreCase))
                {
                    result = ResponseType.Json;
                }
            }

            return result;
        }

        public static string ToJSON(this object obj)
        {
            if (obj == null)
                return null;

            object[] attribs = obj.GetType().GetCustomAttributes(typeof(DataContractAttribute), true);
            if (attribs != null && attribs.Length > 0)
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                using (MemoryStream ms = new MemoryStream())
                {
                    serializer.WriteObject(ms, obj);
                    return Encoding.Default.GetString(ms.ToArray());
                }
            }
            // OBSOLETE APPROACH - takes about any object and serializes that, output is not as clean as with DataContractJsonSerializer though
            return new JavaScriptSerializer().Serialize(obj);
        }

        public static string ToXml(this object obj)
        {
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            StringBuilder sb = new StringBuilder();
            using (StringWriter sw = new StringWriter(sb))
            {
                serializer.Serialize(sw, obj);
            }

            return sb.ToString();
        }

        public static bool IsSerializable(this object obj)
        {
            // Check for the Serializable type attribute 
            return ((obj.GetType().Attributes & TypeAttributes.Serializable) == TypeAttributes.Serializable);
        }
    }
}
